package com.market.controller.admin;

import com.market.bean.MarketCoupon;
import com.market.bean.MarketCouponUser;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.exception.ValidationException;
import com.market.service.admin.AdminCouponService;
import com.market.utils.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author fan
 * @Description: 优惠卷管理
 * @since 2022/12/29 14:10
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {
    @Autowired
    AdminCouponService couponService;

    @RequiresPermissions("admin:coupon:list")
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo info, String name, Short type, Short status) {
        CommonData<MarketCoupon> data = couponService.list(info, name, type, status);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:coupon:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated MarketCoupon coupon, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketCoupon data = couponService.create(coupon);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated MarketCoupon coupon, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketCoupon data = couponService.update(coupon);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon coupon) {
        couponService.delete(coupon);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:coupon:listuser")
    @GetMapping("listuser")
    public BaseRespVo listUser(BasePageInfo info, Integer couponId, Integer userId, Short status) {
        CommonData<MarketCouponUser> data = couponService.listUser(info, couponId, userId, status);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("read")
    public BaseRespVo read(Integer id) {
        MarketCoupon data = couponService.read(id);
        return BaseRespVo.ok(data);
    }
}
